﻿#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
using namespace std;

/*
operator new：该函数实际通过malloc来申请空间，当malloc申请空间成功时直接返回；申请空间
失败，尝试执行空        间不足应对措施，如果改应对措施用户设置了，则继续申请，否
则抛异常。
*/
//void *__CRTDECL operator new(size_t size) _THROW1(_STD bad_alloc)
//{
//	// try to allocate size bytes
//	void *p;
//	while ((p = malloc(size)) == 0)
//	if (_callnewh(size) == 0)
//	{
//		// report no memory
//		// 如果申请内存失败了，这里会抛出bad_alloc 类型异常
//		static const std::bad_alloc nomem;
//		_RAISE(nomem);
//	}
//	return (p);
//}
///*
//operator delete: 该函数最终是通过free来释放空间的
//*/
//void operator delete(void *pUserData)
//{
//	_CrtMemBlockHeader * pHead;
//	RTCCALLBACK(_RTC_Free_hook, (pUserData, 0));
//	if (pUserData == NULL)
//		return;
//	_mlock(_HEAP_LOCK);  /* block other threads */
//	__TRY
//		    /* get a pointer to memory block header */
//			pHead = pHdr(pUserData);
//	     /* verify block type */
//	_ASSERTE(_BLOCK_TYPE_IS_VALID(pHead->nBlockUse));
//	_free_dbg(pUserData, pHead->nBlockUse);
//	__FINALLY
//		_munlock(_HEAP_LOCK);  /* release other threads */
//	__END_TRY_FINALLY
//		return;
//}
///*
//free的实现
//*/
//#define  free(p)        _free_dbg(p, _NORMAL_BLOCK)









class Stack
{
public:
	Stack(int capacity = 4)
	{
		cout << "Stack(int capacity = 4)" << endl;

		_a = new int[capacity];
		_top = 0;
		_capacity = capacity;
	}

	//~Stack()
	//{
	//	cout << "~Stack()" << endl;

	//	delete[] _a;
	//	_a = nullptr;
	//	_top = 0;
	//	_capacity = 0;
	//}
private:
	int* _a;
	int  _top;
	int  _capacity;
};

class A
{
public:
	A(int a = 0)
		: _a(a)
	{
		cout << "A():" << this << endl;
	}

	~A()
	{
		cout << "~A():" << this << endl;
	}

private:
	int _a;
};

int main()
{

	Stack* p2 = (Stack*)operator new(sizeof(Stack));
	operator delete(p2);

	Stack* p3 = new Stack[10];
	delete[] p3;

		A* p1 = (A*)operator new(sizeof(A));
	
		// 不能这么显示调用构造函数
		//p1->A(1);
		// 但是可以用定位new显示调用构造函数
		new(p1)A();
		new(p1)A;
		//	// 析构函数可以显示调用
		p1->~A();
		operator delete(p1);

	return 0;
}